package com.example.deepschool.Model;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Date;

@Data
public class User implements Serializable {
	
	@Schema(description = "用户ID")
    private Long id;
	
	@NotBlank(message = "用户名不能为空")
    @Length(min = 4, max = 50, message = "用户名长度必须在4-50之间")
	@Schema(description = "用户名",
	        minLength = 4,
	        maxLength = 50)
    private String username;
	
	@Schema(description = "昵称")
    private String nickname;
	
	@Schema(description = "邮箱地址")
    private String email;
	
	@NotBlank(message = "密码不能为空")
    @Length(min = 6, max = 20, message = "密码长度必须在6-20之间")
	@Schema(description = "密码",
	        minLength = 6,
	        maxLength = 20)
    private String password;
	
	@Schema(description = "权限标识")
    private Long permissions;
	
	@Schema(description = "所属组织ID")
    private Long organizationId;
	
	@Schema(description = "注册时间")
    private Date registrationTime;
	
	@Schema(description = "用户头像URL")
    private String avatarUrl;
	
	/**
	 * 将JSON字符串转换为User对象。
	 *
	 * @param json JSON字符串
	 * @return User对象实例
	 */
    public static User fromJson(String json) {
	    Gson gson = new GsonBuilder().create();
        return gson.fromJson(json, User.class);
    }
	
	/**
	 * 将User对象转换为JSON字符串。
	 *
	 * @return JSON字符串
	 */
    public String toJson() {
	    Gson gson = new GsonBuilder().create();
        return gson.toJson(this);
    }
}
